
//
//  ZTOrderTableViewCell.m
//  FD
//
//  Created by xiaofang wang on 2019/1/18.
//  Copyright © 2019年 ZhiYuan Network. All rights reserved.
//

#import "ZTOrderTableViewCell.h"
//model
#import "ZTOrderModel.h"

@interface ZTOrderTableViewCell()
@property (weak, nonatomic) IBOutlet UILabel *nameLb; //店名字
@property (weak, nonatomic) IBOutlet UILabel *timeLb; //取餐时间
@property (weak, nonatomic) IBOutlet UIImageView *iconIv; //头像
@property (weak, nonatomic) IBOutlet UILabel *canPinCountLb; //餐品数量
@property (weak, nonatomic) IBOutlet UILabel *priceLb; //餐品价格
@property (weak, nonatomic) IBOutlet UILabel *stateLb; //状态(待取餐、立即支付、代付款)
@property (weak, nonatomic) IBOutlet UIButton *leftButton; //左边按钮
@property (weak, nonatomic) IBOutlet UIButton *rightButton; //右边按钮
@end
@implementation ZTOrderTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.timeLb.hidden = YES; //隐藏
    self.priceLb.hidden = YES; //隐藏
    
    
    //设置字体颜色
    self.nameLb.textColor = kLightGrayTextColor;
    
    //左右按钮变宽
    self.leftButton.layer.masksToBounds = YES;
    self.leftButton.layer.cornerRadius = 5;
    self.leftButton.layer.borderWidth = 1.0;
    self.leftButton.layer.borderColor = kMainLightGrayColor.CGColor;
    
    self.rightButton.layer.masksToBounds = YES;
    self.rightButton.layer.cornerRadius = 5;
    self.rightButton.layer.borderWidth = 1.0;
    self.rightButton.layer.borderColor = kMainLightGrayColor.CGColor;
    
    //取消点击样式
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


#pragma mark - 一 点击事件
- (IBAction)clickLeftBtnAction:(UIButton *)sender {
    if (self.clickLeftBtnBlcok) {
        self.clickLeftBtnBlcok();
    }
}

- (IBAction)clickRightBtnAction:(UIButton *)sender {
    if (self.clickRightBtnBlcok) {
        self.clickRightBtnBlcok();
    }
}


#pragma mark - 二 设置数据
- (void)setModel:(ZTOrderModel *)model {
    _model = model;
    
    //0.门店
    
    self.nameLb.text = [NSString stringWithFormat:@"%@:%@",NSLocalizedString(MengDian, @"门店"),model.name];
    
    //1.取餐时间
    self.timeLb.text = [NSString stringWithFormat:@"取餐时间:%@",[self timeStrFromTimeStamp:model.createTime]];

    //1-19这里也有坑。:18刚好。换成:20就会出问题。
//    self.timeLb.text = [NSString stringWithFormat:@"取餐时间:%@",@"2019-01-20 19:20"];
//    2019-01-20 19:20
//    2019-01-20 19:18

    
    //2.图片
    //1-19显示goodsList第一个元素的图片
    if (model.goodsList.count) {
        NSDictionary *firstDic = [model.goodsList firstObject];
        NSString *thumbnail = firstDic[@"thumbnail"];
        NSString *urlStr = [NSString stringWithFormat:@"%@%@",kPicDomain,thumbnail];
        NSURL *imgurl = [NSURL URLWithString:urlStr];
        [self.iconIv sd_setImageWithURL:imgurl];
    }
    
    //3.餐品数量 16号字体
    NSString *countStr = [NSString stringWithFormat:@"%ld",model.count];
  
    //国际化
    NSString *resultStr = [NSString stringWithFormat:@"你本次订单共计%@份餐品",countStr];
    if (![NSString rb_ChineseLanguage]) {
        resultStr = [NSString stringWithFormat:@"Total Order: %@ item",countStr];
    }
    
    self.canPinCountLb.text = resultStr;
    
    //富文本数量红色显示
//    {
//        NSString *countStr = [NSString stringWithFormat:@"%ld",model.count];
//        NSString *resultStr = [NSString stringWithFormat:@"你本次餐品共计%@份餐品",countStr];
//        NSRange redRange = [resultStr rangeOfString:countStr];
//
//        UIColor *blackColor = [UIColor blackColor];
//        UIColor *redColor = kMainRedColor;
//        UIColor *clearColor = [UIColor clearColor];
//
//        NSDictionary *attDic = @{NSForegroundColorAttributeName:blackColor,NSFontAttributeName:[UIFont systemFontOfSize:16.0]};
//
//        NSMutableAttributedString *attstrM = [[NSMutableAttributedString alloc] initWithString:resultStr attributes:attDic];
//        [attstrM yy_setTextHighlightRange:redRange color:redColor backgroundColor:clearColor userInfo:nil];
//
//        self.canPinCountLb.attributedText = attstrM;
//    }
    
   
//    备注：
//    status字段说明：
//    当delivery_type=0时：0：待付款 1：已下单 2：制作中 3：派送中 4：待评价 5：已评价 6：申请退款中 7：退款处理中 8：订单关闭 9：订单关闭
//    当delivery_type=1时：0：待付款 1：待取餐 4：待评价 5：已评价 6：申请退款中 7：退款处理中 8：订单关闭 9：订单关闭
//    当paymentType=1时，不显示“支付”按钮
    
    //价格
    self.priceLb.text = [NSString stringWithFormat:@"€%.1f",[model.actualAmount floatValue]];
    
    FDLog(@"model.status = %@",model.status);
    
    //4.订单状态
    [self setupOrdetState:model.status];
    
//    model.paymentType = @(1);
//    model.status = @(1);
    //5.按钮文字、样式、点击事件
    [self setupLeftRightBtnDispayLoginWithPaymentType:model.paymentType status:model.status];
}


//!!!!:1-22 订单状态文字
- (void)setupOrdetState:(NSNumber *)status {
    
    if ([status isEqualToNumber:@(0)]) {
        self.stateLb.text = NSLocalizedString(Order_Pending, @"待付款");
    }else if ([status isEqualToNumber:@(1)]) {
        self.stateLb.text = NSLocalizedString(L_Order_YiXiaDan, @"已下单");
    }else if ([status isEqualToNumber:@(2)]) {
        self.stateLb.text = NSLocalizedString(L_Order_DaiQuCan, @"待取餐");
    }else if ([status isEqualToNumber:@(4)]) {
        self.stateLb.text = NSLocalizedString(Order_Evaluate, @"待评价");
    }else if ([status isEqualToNumber:@(5)]) {
        self.stateLb.text = NSLocalizedString(Evaluated, @"已评价");
    }else if ([status isEqualToNumber:@(6)]) {
        self.stateLb.text = NSLocalizedString(L_Order_ShenQinTuikuanZhong, @"申请退款中");
    }else if ([status isEqualToNumber:@(7)]) {
        self.stateLb.text = NSLocalizedString(L_Order_TuiKuanChuLiZhong, @"退款处理中");
    }else if ([status isEqualToNumber:@(8)] ||
              [status isEqualToNumber:@(9)]) {
        self.stateLb.text = NSLocalizedString(L_Order_Closed, @"订单关闭");
    }
}


//!!!!:1-22设置左右按钮显示逻辑
- (void)setupLeftRightBtnDispayLoginWithPaymentType:(NSNumber *)payment status:(NSNumber *)status {
    NSString *zlydStr = NSLocalizedString(Order_OrderAgain, @"再来一单");

   //0.什么时候显示两个按钮(再来一单,评价)
    if ([status isEqualToNumber:@(4)]) {
        self.leftButton.hidden = NO; //显示
        [self.leftButton setTitle:zlydStr forState:(UIControlStateNormal)];
        [self setLightGrayButton:self.leftButton];
        
        [self.rightButton setTitle:NSLocalizedString(L_Order_Evaluate, @"评价")  forState:(UIControlStateNormal)];
        [self setLightRedButton:self.rightButton]; //浅红
    }else {
        self.leftButton.hidden = YES; //只报留一个按钮
    }
    
    
    //再来一单
    if ([status isEqualToNumber:@(5)] ||
        [status isEqualToNumber:@(6)] ||
        [status isEqualToNumber:@(7)] ||
        [status isEqualToNumber:@(8)] ||
        [status isEqualToNumber:@(9)]) {
        [self.rightButton setTitle:zlydStr forState:(UIControlStateNormal)];
        [self setLightGrayButton:self.rightButton];
    }
    
    //2019-2-21 payment ==0 && status ==3 再来一单
    if ([payment isEqualToNumber:@(3)]) {
        [self.rightButton setTitle:zlydStr forState:(UIControlStateNormal)];
        [self setLightGrayButton:self.rightButton];
    }
    
    //2019-2-21 status=1,2,3 再来一单
    if ([status isEqualToNumber:@(1)] ||
        [status isEqualToNumber:@(2)]) {
        [self.rightButton setTitle:zlydStr forState:(UIControlStateNormal)];
        [self setLightGrayButton:self.rightButton];
    }
    
    
    
    
    //立即支付
    NSString *ljzfStr = NSLocalizedString(Order_PayNow, @"立即支付");
    if ([payment isEqualToNumber:@(0)] &&
        [status isEqualToNumber:@(0)]) {
        [self.rightButton setTitle:ljzfStr forState:(UIControlStateNormal)];
        [self setLightGrayButton:self.rightButton];
    }
    if ([payment isEqualToNumber:@(1)]) {
        if ([status isEqualToNumber:@(0)] ||
            [status isEqualToNumber:@(1)] ||
            [status isEqualToNumber:@(2)] ||
            [status isEqualToNumber:@(3)]) {
            [self.rightButton setTitle:ljzfStr forState:(UIControlStateNormal)];
            [self setLightRedButton:self.rightButton];
        }
    }
    
    //2019-2-22原有逻辑不变 新加逻辑
    //paymentType=0，status=0，支付按钮显示灰色，改为红色
    //paymentType=1，status=1、2，支付按钮显示红色，但是点击无反应
    if ([payment isEqualToNumber:@(0)] && [status isEqualToNumber:@(0)]) {
        [self setLightRedButton:self.rightButton];
    }
}
    



#pragma mark - 三 private
/**更新按钮文字状态 */
- (void)setLightGrayButton:(UIButton *)button {
    
    //灰色：999999
    UIColor *hui = [UIColor rb_colorHex:0x999999];
    [button setTitleColor:hui forState:(UIControlStateNormal)];
    button.layer.borderColor = hui.CGColor;
}

- (void)setLightRedButton:(UIButton *)button {
    //红色：e54b4b
    UIColor *r = [UIColor rb_colorHex:0xe54b4b];
    [button setTitleColor:r forState:(UIControlStateNormal)];
    button.layer.borderColor = r.CGColor;
}

- (NSString *)timeStrFromTimeStamp:(NSNumber *)timestamp {

    NSDateFormatter *formater = [[NSDateFormatter alloc] init];
    [formater setDateFormat:@"yyyy-MM-dd HH:mm"];
    
    NSTimeInterval interval = [timestamp floatValue] / 1000;

    NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSString *timeStr = [formater stringFromDate:date];
    
    FDLog(@"timeStr:%@",timeStr);
    return timeStr;
}

#pragma mark - 富文本
/**
 
 //
 //
 //        //富文本
 //        UIColor *blackColor = [UIColor blackColor];
 //        UIColor *redColor = [UIColor redColor];
 //        UIColor *clearColor = [UIColor clearColor];
 //
 //        //属性
 //        NSDictionary *attDic = @{NSForegroundColorAttributeName:redColor,NSFontAttributeName:[UIFont systemFontOfSize:16.0*kScaleFit]};
 //        //富文本
 //        NSMutableAttributedString *attM = [[NSMutableAttributedString alloc] initWithString:resultStr attributes:attDic];
 //        [attM yy_setTextHighlightRange:centerRange color:blackColor backgroundColor:clearColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
 //        }];
 //
 //        //返回
 //        return attM;
 //    }
 //}
 */

@end
